<?php

class UpdateCommand extends CConsoleCommand {

    public function run($args) {
	//send mail for birthdate
//	$this->sendMailForBirth();
	//send mail for match
//	$this->sendMailForMatch();
//	$this->_sendMailToExpired();
	$this->_sendMailActiveBeforeWeek();
    }

    private function sendMailForBirth() {
	$day = date('d', time());
	$month = date('m', time());
	$i = 0;
	//get all user has birthdate today
	$aUsers = db()->createCommand('SELECT * FROM {{users}} WHERE DAY(`birthday`)=' . $day . ' AND MONTH(`birthday`)=' . $month)->queryAll();

	if ($aUsers) {
	    foreach ($aUsers as $user) {
		$i++;
		if ($i == 30) {
		    sleep(30);
		    $i = 0;
		}
		$receipt = array(
		    'username' => $user['username'],
		    'email' => $user['email']
		);
		EmailHelper::sendMailForBirth($receipt);
	    }
	}
    }

    private function sendMailForMatch() {
	//get all users
	$model = IdealProfile::model()->with('user')->findAll();
	$day = date('d', time());
	$month = date('m', time());
	$year = date('Y', time());
	$daySql = 'DAY(FROM_UNIXTIME(`joined`))';
	$monthSql = 'MONTH(FROM_UNIXTIME(`joined`))';
	$yearSql = 'YEAR(FROM_UNIXTIME(`joined`))';
	$i = 0;
	foreach ($model as $item) {
	    //get list match users
	    $aUsers = db()->createCommand('SELECT * FROM {{users}} 
					    WHERE gender!="' . $item->user->gender . '" AND ' . $daySql . '=' . $day . ' AND ' . $monthSql . '=' . $month . ' AND ' . $yearSql . '=' . $year
			    . ' AND marital="' . $item->marital . '" AND ethnicity="' . $item->ethnicity . '" AND relationship="' . $item->relationship
			    . '" AND zodiac="' . $item->zodiac . '" AND smoking="' . $item->smoking . '" 
				AND height BETWEEN ' . $item->height_from . ' AND ' . $item->height_to . ' AND general_build="' . $item->general_build
			    . '" AND children="' . $item->children . '" AND religion="' . $item->religion . '" AND age BETWEEN ' . $item->age_from . ' AND ' . $item->age_to
		    )->queryAll();
	    if ($aUsers) {
		$i++;
		if ($i == 30) {
		    sleep(30);
		    $i = 0;
		}
		$receipt = array(
		    'username' => $item->user->username,
		    'email' => $item->user->email,
		    'listUsers' => $aUsers
		);
		EmailHelper::sendmailFormatch($receipt);
	    }
	}
    }

    /*
     * send mail to users have expired 
     */

    private function _sendMailToExpired() {
	//get all ads expire and have not sent yet
	$model = User::model()->findAll(array(
	    'condition' => 't.expired_time_msg > 0 and t.expired_time_msg<=:time and t.is_send_mail=:is_send_mail',
	    'params' => array(
		':time' => time(),
		':is_send_mail' => User::NOT_SEND_MAIL
	    )
	));
	$i = 0;
	foreach ($model as $item) {
	    if ($i == 30) {
		$i = 0;
		sleep(30);
	    }
	    $urlActive = "<a href='" . SITE_NAME_URL . "paypal/re_active'> here </a>";
	    EmailHelper::sendReactive($item->email, $item->username, $urlActive);
	    //update to database is send
	    $item->is_send_mail = User::SEND_MAIL;
	    $item->save();
	}
    }

    /**
     * send mail before 1 weeks
     */
    private function _sendMailActiveBeforeWeek() {
	$timeBefore = time() + 7 * 24 * 60 * 60;

	//get all ads expire and have not sent yet
	$model = User::model()->findAll(array(
	    'condition' => 't.expired_time_msg > 0 and t.expired_time_msg<=:time and t.is_send_mail_before=:is_send_mail_before',
	    'params' => array(
		':time' => $timeBefore,
		':is_send_mail_before' => User::NOT_SEND_MAIL
	    )
	));

	$i = 0;
	foreach ($model as $item) {
	    if ($i == 30) {
		$i = 0;
		sleep(30);
	    }
	    $urlActive = "<a href='" . SITE_NAME_URL . "paypal/re_active'> here </a>";
	    EmailHelper::sendReactiveBefore($item->email, $item->username, $urlActive);
	    //update to database is send
	    $item->is_send_mail_before = User::SEND_MAIL;
	    $item->save();
	}
    }

}

